package space.sakana.edusysback.service;

import com.alibaba.fastjson2.JSON;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import space.sakana.edusysback.emu.QuestionEnum;
import space.sakana.edusysback.entity.Admin;
import space.sakana.edusysback.entity.job.Job;
import space.sakana.edusysback.entity.job.JobPushItem;
import space.sakana.edusysback.entity.job.JobTarget;
import space.sakana.edusysback.entity.paper.FullPaper;
import space.sakana.edusysback.entity.paper.Paper;
import space.sakana.edusysback.entity.paper.PaperItem;
import space.sakana.edusysback.entity.question.Question;
import space.sakana.edusysback.entity.question.type.EssayQuestion;
import space.sakana.edusysback.entity.question.type.MultipleChoice;
import space.sakana.edusysback.entity.question.type.SingleChoice;
import space.sakana.edusysback.entity.question.type.TrueFalseQ;
import space.sakana.edusysback.mapper.AdminMapper;
import space.sakana.edusysback.mapper.job.JobMapper;
import space.sakana.edusysback.mapper.paper.PaperItemMapper;
import space.sakana.edusysback.mapper.paper.PaperMapper;
import space.sakana.edusysback.mapper.question.QuestionMapper;
import space.sakana.edusysback.result.DataResult;
import space.sakana.edusysback.result.Result;
import space.sakana.edusysback.util.CookieUtil;

import java.util.List;

@Service
public class PaperService {

    private PaperMapper paperMapper;
    private PaperItemMapper paperItemMapper;
    private QuestionMapper questionMapper;
    private AdminMapper adminMapper;
    private JobMapper jobMapper;

    @Autowired
    public void autowiredMapper(PaperMapper paperMapper, PaperItemMapper paperItemMapper,
                                QuestionMapper questionMapper, AdminMapper adminMapper, JobMapper jobMapper){
        this.paperMapper = paperMapper;
        this.paperItemMapper = paperItemMapper;
        this.questionMapper = questionMapper;
        this.adminMapper = adminMapper;
        this.jobMapper = jobMapper;
    }

    public Result getPaper(int pid, HttpServletRequest request, HttpServletResponse response) {
        Admin admin = adminMapper.selectAdminByToken(CookieUtil.getCookieValueByKey("token", request));
        if (admin==null){
            return Result.failResult;
        }

        return new DataResult(JSON.toJSONString(paperMapper.selectPaperById(pid)));
    }

    public Result getAllPaper(HttpServletRequest request, HttpServletResponse response) {
        Admin admin = adminMapper.selectAdminByToken(CookieUtil.getCookieValueByKey("token", request));
        if (admin==null){
            return Result.failResult;
        }

        return new DataResult(JSON.toJSONString(paperMapper.selectAllPaper()));
    }

    public Result addPaper(String data, HttpServletRequest request, HttpServletResponse response) {
        Admin admin = adminMapper.selectAdminByToken(CookieUtil.getCookieValueByKey("token", request));
        if (admin==null){
            return Result.failResult;
        }

        return new Result(paperMapper.insertPaper(JSON.parseObject(data, Paper.class)));
    }

    public Result updatePaper(String data, HttpServletRequest request, HttpServletResponse response) {
        Admin admin = adminMapper.selectAdminByToken(CookieUtil.getCookieValueByKey("token", request));
        if (admin==null){
            return Result.failResult;
        }

        return new Result(paperMapper.updatePaper(JSON.parseObject(data, Paper.class)));
    }

    public Result getPaperItems(int pid, HttpServletRequest request, HttpServletResponse response) {
        Admin admin = adminMapper.selectAdminByToken(CookieUtil.getCookieValueByKey("token", request));
        if (admin==null){
            return Result.failResult;
        }

        List<PaperItem> paperItems = paperItemMapper.selectPaperItemByPid(pid);
        for (int i = 0; i < paperItems.size(); i++) {
            PaperItem paperItem = paperItems.get(i);
            Question question = questionMapper.selectQuestionById(paperItem.getQid());
            paperItem.filePaperItem(question);
        }
        return new DataResult(JSON.toJSONString(paperItems));
    }

    public Result addPaperItem(int pid, String data, HttpServletRequest request, HttpServletResponse response) {
        Admin admin = adminMapper.selectAdminByToken(CookieUtil.getCookieValueByKey("token", request));
        if (admin==null){
            return Result.failResult;
        }

        PaperItem paperItem = JSON.parseObject(data, PaperItem.class);
        Question question = questionMapper.selectQuestionById(paperItem.getQid());
        if (question==null){
            return Result.failResult;
        }
        return new Result(paperItemMapper.insertPaperItem(paperItem));
    }

    public Result updatePaperItem(int pid, String data, HttpServletRequest request, HttpServletResponse response) {
        Admin admin = adminMapper.selectAdminByToken(CookieUtil.getCookieValueByKey("token", request));
        if (admin==null){
            return Result.failResult;
        }

        return new Result(paperItemMapper.updatePaperItem(JSON.parseObject(data, PaperItem.class)));
    }

    public Result getPaperItemByJobTarget(int id, HttpServletRequest request, HttpServletResponse response) {
        JobTarget jobTarget = jobMapper.selectJobTarget(id);
        Job job = jobMapper.selectJobById(jobTarget.getJid());
        List<PaperItem> paperItems = paperItemMapper.selectPaperItemByPid(job.getPid());
        FullPaper fullPaper = new FullPaper();
        fullPaper.setPid(job.getPid());
        for (PaperItem paperItem :paperItems) {
            Question question = questionMapper.selectQuestionById(paperItem.getQid());
            if (question==null){
                continue;
            }
            JobPushItem jobPushItem = jobMapper.selectJobPushItem(jobTarget.getJid(), jobTarget.getUid(), paperItem.getQid());
            if (jobPushItem==null){
                continue;
            }
            if (question.getType()== QuestionEnum.SINGLE_CHOICE.getQuestionInt()){
                SingleChoice singleChoice = questionMapper.selectQuestionAById(question.getId());
                if (singleChoice==null){
                    continue;
                }
                singleChoice.setScore(paperItem.getScore());
                singleChoice.setNowScore(jobPushItem.getScore());
                fullPaper.addSingleChoice(singleChoice);
            }
            else if (question.getType()==QuestionEnum.MULTIPLE_CHOICE.getQuestionInt()){
                MultipleChoice multipleChoice = questionMapper.selectQuestionBById(question.getId());
                if (multipleChoice==null){
                    continue;
                }
                multipleChoice.setScore(paperItem.getScore());
                multipleChoice.setNowScore(jobPushItem.getScore());
                fullPaper.addMultipleChoice(multipleChoice);
            }
            else if (question.getType()==QuestionEnum.TRUE_FALSE_Q.getQuestionInt()){
                TrueFalseQ trueFalseQ = questionMapper.selectQuestionCById(question.getId());
                if (trueFalseQ==null){
                    continue;
                }
                trueFalseQ.setScore(paperItem.getScore());
                trueFalseQ.setNowScore(jobPushItem.getScore());
                fullPaper.addTrueFalseQ(trueFalseQ);
            }
            else if (question.getType()==QuestionEnum.ESSAY_QUESTION.getQuestionInt()){
                EssayQuestion essayQuestion = questionMapper.selectQuestionDById(question.getId());
                if (essayQuestion==null){
                    continue;
                }
                essayQuestion.setScore(paperItem.getScore());
                essayQuestion.setNowScore(jobPushItem.getScore());
                fullPaper.addEssayQuestion(essayQuestion);
            }
        }
        return new DataResult(JSON.toJSONString(fullPaper));
    }

    public Result deletePaper(int pid, HttpServletRequest request, HttpServletResponse response) {
        Admin admin = adminMapper.selectAdminByToken(CookieUtil.getCookieValueByKey("token", request));
        if (admin==null){
            return Result.failResult;
        }

        return new Result(paperMapper.deletePaperById(pid));
    }
}
